package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.Member;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MemberDao {
    private final JdbcTemplate jdbcTemplate;

    public MemberDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Member getUserByUsername(String username) {
        String sql = "SELECT * FROM tz_member WHERE username = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(Member.class));
    }

    public List<Member> getAllUsers() {
        String sql = "SELECT * FROM tz_member";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Member.class));
    }

    public Member getUserById(String id) {
        String sql = "SELECT id,username FROM tz_member WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Member.class));
    }
}
